package ordinaryArray;

import org.junit.Test;

/*
 *@description 整数转罗马数字
 *@author hkfan
 *@version 1.0
 *@create 2025/5/27 09:19
 */
public class LeetCode_12 {


    @Test
    public void test(){
        System.out.println(intToRoman(4));
    }

    public String intToRoman(int num) {
        return solution_1(num);
    }

    private String solution_1(int num) {
        //1. 递归返回条件
        if (num==0){
            return "";
        }
        //2. 递归体
        int[] dig= calDig(num); //0：位数，1：首位数
        if (dig[1]==4){
            if (dig[0]==1){
                return "IV"+solution_1(num-4);
            } else if (dig[0]==2) {
                return "XL"+solution_1(num-40);
            }else if (dig[0]==3){
                return "CD"+solution_1(num-400);
            }else {
                return "M"+solution_1(num-1000);
            }
        } else if (dig[1]==9) {
            if (dig[0]==1){
                return "IX"+solution_1(num-9);
            } else if (dig[0]==2) {
                return "XC"+solution_1(num-90);
            }else if (dig[0]==3){
                return "CM"+solution_1(num-900);
            }else {
                return "M"+solution_1(num-1000);
            }
        }else if (dig[1]>=5 ){
            if (dig[0]==1){
                return "V"+solution_1(num-5);
            } else if (dig[0]==2) {
                return "L"+solution_1(num-50);
            }else  if (dig[0]==3){
                return "D"+solution_1(num-500);
            }else {
                return "M"+solution_1(num-1000);
            }
        }else {
            if (dig[0]==1){
                return "I"+solution_1(num-1);
            } else if (dig[0]==2) {
                return "X"+solution_1(num-10);
            }else if (dig[0]==3){
                return "C"+solution_1(num-100);
            }else {
                return "M"+solution_1(num-1000);
            }
        }

    }

    private int[] calDig(int num) {

        int temp = num;
        int[] res = new int[2];
        while (temp!=0){
            res[0]++;
            temp/=10;
        }
        for (int i = 0; i < res[0]-1; i++) {
            num/=10;
        }
        res[1]=num;
        return res;
    }


}
